package com.soccertgc.utils
{
	import flash.globalization.DateTimeFormatter;
	import flash.globalization.DateTimeStyle;
	import flash.globalization.LastOperationStatus;
	import flash.globalization.LocaleID;

	public class DateUtils
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		
		// http://tinyurl.com/8kp39tn for 
		
		/**
		 * Standard invalid date, to use to signal and unknown date 
		 */
		public static const INVALID_DATE:Date						= new Date(1900, 0, 0, 0, 0, 0, 0);
		
		/**
		 * Default format to use to parse and format date
		 */		
		public static const	MX_DEFAULT_DATE_FORMAT:String				= "YYYY/MM/DD";
		
		/**
		 * Default format to use to parse and format time 
		 */		
		public static const	MX_DEFAULT_TIME_FORMAT:String				= "JJ:NN:SS";
		
		/**
		 * Default format to use to parse and format date and time 
		 */
		public static const	MX_DEFAULT_DATETIME_FORMAT:String			= "YYYY/MM/DD JJ:NN:SS";
		
		/**
		 * Default format to use to parse and format date
		 */		
		public static const	DEFAULT_DATE_FORMAT:String				= "yyyy/MM/dd";
		
		/**
		 * Default format to use to parse and format time 
		 */		
		public static const	DEFAULT_TIME_FORMAT:String				= "HH:mm:ss.SSS";
		
		/**
		 * Default format to use to parse and format date and time 
		 */
		public static const	DEFAULT_DATETIME_FORMAT:String			= "yyyy/MM/dd HH:mm:ss.SSS";
		
		// -------------------------------------------------------------------------------
		// METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public static function timestamp():String
		{               
			return format(new Date(), DEFAULT_DATETIME_FORMAT);
		}
		
		/**
		 * Calculated the difference between 2 dates in ms 
		 * @param dOld
		 * @param dNew
		 * @return 
		 * 
		 */		
		public static function elapsed(dOld:Date, dNew:Date = null):Number
		{
			// Date as milliseconds
			var timeOld:Number = dOld.time;
			var timeNew:Number = dNew!=null ? dNew.time : (new Date().time);
			// returns the difference in ms
			return timeNew - timeOld;
		}
		
		/**
		 * Formats a date 
		 * @param date
		 * @param format
		 * @return 
		 * 
		 */		
		public static function format(date:Date, format:String = DEFAULT_DATETIME_FORMAT):String
		{
			// Old formatting with Flex framework
			// var formatter:DateFormatter = new DateFormatter();
			// formatter.formatString = format;
			// return formatter.format(date);
			
			// LT_FIX: patch to avoid milliseconds problem
			var msCount:uint = StringUtils.countOf(format, "S", true);
			if (msCount > 0)
			{
				format = format.replace(StringUtils.repeat("S", msCount), StringUtils.padLeft(date.milliseconds.toString(), "0", msCount));
			}
			
			// Creates the formatter
			var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT);
			formatter.setDateTimePattern(format);
			// Do date formatting
			var output:String = formatter.format(date);
			// Chek formatting execution
			if (formatter.lastOperationStatus == LastOperationStatus.NO_ERROR) return output;
			else throw new Error("Error formatting a date using format '" + format + "' : " +  formatter.lastOperationStatus); 
		}
		
		/**
		 * 
		 * @param data
		 * @return 
		 */		
		public static function parse(timestamp:String):Date
		{
			// return DateFormatter.parseDateString(data);
			var d:Date = new Date();
			var ms:Number = Date.parse(timestamp);
			d.time = ms; 
 			return d;
		}
		
	}
}